package edu.taru.interceptor;

import edu.taru.pojo.Menu;
import edu.taru.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

public class DownloadInterceptor implements HandlerInterceptor {
    /**
     * 进入之前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


        String code = request.getParameter("code");
        HttpSession session = request.getSession(false);
        List<String> codes = (List<String>) session.getAttribute("codes");
        for(int i = 0; i < codes.size(); i++){
            if(code.equals(codes.get(i))){
                return true;
            }
        }
        response.sendRedirect(request.getContextPath()+"/404");
        //response.sendError(403,"抱歉，你没有权限！（想偷偷的弄公司的数据，那是不存在嘀。。。）");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
